package cn.iocoder.yudao.module.member.controller.admin.leaveword;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.module.member.controller.admin.leaveword.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.leaveword.LeaveWordDO;
import cn.iocoder.yudao.module.member.service.leaveword.LeaveWordService;

@Tag(name = "管理后台 - 用户留言")
@RestController
@RequestMapping("/member/leave-word")
@Validated
public class LeaveWordController {

    @Resource
    private LeaveWordService leaveWordService;

    @PostMapping("/create")
    @Operation(summary = "创建用户留言")
    @PreAuthorize("@ss.hasPermission('member:leave-word:create')")
    public CommonResult<Long> createLeaveWord(@Valid @RequestBody LeaveWordSaveReqVO createReqVO) {
        return success(leaveWordService.createLeaveWord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户留言")
    @PreAuthorize("@ss.hasPermission('member:leave-word:update')")
    public CommonResult<Boolean> updateLeaveWord(@Valid @RequestBody LeaveWordSaveReqVO updateReqVO) {
        leaveWordService.updateLeaveWord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户留言")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:leave-word:delete')")
    public CommonResult<Boolean> deleteLeaveWord(@RequestParam("id") Long id) {
        leaveWordService.deleteLeaveWord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户留言")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:leave-word:query')")
    public CommonResult<LeaveWordRespVO> getLeaveWord(@RequestParam("id") Long id) {
        LeaveWordDO leaveWord = leaveWordService.getLeaveWord(id);
        return success(BeanUtils.toBean(leaveWord, LeaveWordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户留言分页")
    @PreAuthorize("@ss.hasPermission('member:leave-word:query')")
    public CommonResult<PageResult<LeaveWordRespVO>> getLeaveWordPage(@Valid LeaveWordPageReqVO pageReqVO) {
        PageResult<LeaveWordDO> pageResult = leaveWordService.getLeaveWordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, LeaveWordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户留言 Excel")
    @PreAuthorize("@ss.hasPermission('member:leave-word:export')")
    public void exportLeaveWordExcel(@Valid LeaveWordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<LeaveWordDO> list = leaveWordService.getLeaveWordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户留言.xls", "数据", LeaveWordRespVO.class,
                        BeanUtils.toBean(list, LeaveWordRespVO.class));
    }

}